package com.qf58.search.docview;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qf58.search.commons.ESConnect;
import com.qf58.search.utils.JsonUtils;
import org.apache.commons.codec.Charsets;
import org.apache.commons.io.IOUtils;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhubo on 2017/8/14.
 */
public class DoMain {
    public static void main(String[] args) throws Exception {
        String index = "geo-2017.08.10";
        String type = "geo_type";



        //TransportClient client = ESConnect.getInstance().getTransportClient();
        //IndexMaintainUtils.createIndexBasic(client,index,1,1);
        //IndexMaintainUtils.updateReplicas(client,index,2);
        //IndexMaintainUtils.createWithSetting(client,index,"esjson/demo_settings.json");

        //IndexMaintainUtils.createMapping(client,index,"geo_type","esjson/demo_mapping.json");
        //删除索引数据
        //IndexMaintainUtils.deleteIndex(client,index);

        //ESBaseCrud.basicAdd(client,index,type,null,obj);

       /* MyObj obj1 = new MyObj();
        obj1.setId(1);
        obj1.setName("张三");
        MyObj obj2 = new MyObj();
        obj2.setId(2);
        obj2.setName("张三");
        MyObj obj3 = new MyObj();
        obj3.setId(3);
        obj3.setName("王五");
        MyObj obj4 = new MyObj();
        obj4.setId(4);
        obj4.setName("李四");
        List<Object> list = new ArrayList<>();
        list.add(obj1);
        list.add(obj2);
        list.add(obj3);
        list.add(obj4);
        ESBaseCrud.batchAdd(client,index,type,list);*/


        /*MyObj obj4 = new MyObj();
        obj4.setId(5);
        obj4.setName("李四");
        ESBaseCrud.basicUpdate(client,index,type,"5",obj4);*/


        /*MatchQueryBuilder builder = QueryBuilders.matchQuery("name","张三");
        ESBaseCrud.deleteByQuery(client,index,builder);*/


        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("2017-08-14");
        System.out.println(date.getTime());*/

        dotest();
    }

    public void createIndex(TransportClient client){
        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        indicesAdminClient.prepareCreate("geo-2017.08.10")
                .setSettings(Settings.builder()
                        .put("index.number_of_shards", 3)
                        .put("index.number_of_replicas",1)
                ).get();
    }

    public void createWithSetting(TransportClient client) throws Exception {
        ClassLoader classLoader = DoMain.class.getClassLoader();
        String jsonStr = IOUtils.toString(classLoader.getResourceAsStream("esjson/demo_settings.json"), Charsets.UTF_8);

        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        indicesAdminClient.prepareCreate("geo-2017.08.10").setSettings(jsonStr, XContentType.JSON).get();
    }

    public void updateSetting(TransportClient client) throws Exception{
        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        indicesAdminClient.prepareUpdateSettings("geo-2017.08.10").setSettings(Settings.builder().put("index.number_of_replicas",2)).get();
    }

    public void createMapping(TransportClient client) throws Exception{
        ClassLoader classLoader = getClass().getClassLoader();
        String jsonStr = IOUtils.toString(classLoader.getResourceAsStream("esjson/demo_mapping.json"), Charsets.UTF_8);
        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        PutMappingResponse response = indicesAdminClient.preparePutMapping("geo-2017.08.10").setType("geo_type").setSource(jsonStr,XContentType.JSON).execute().actionGet();
        System.out.println(response);
    }


    public static void dotest() throws Exception{
        MyObj obj = new MyObj();
        obj.setId("1234");
        obj.setName("张三");
        Gson gson = new GsonBuilder().create();
        String jsonStr = gson.toJson(obj);
        System.out.println(jsonStr);
        String id = (String) JsonUtils.getValue(jsonStr,"id");

        System.out.println(id);
    }

}
